<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/21
 * Time: 10:33
 * @copyright: ©2024 永州微聚商网络科技有限公司
 * @link: https://www.weijuyunke.com
 */

namespace app\admin\controller;

use app\Request;
use app\merchant\model\Poster as PosterModel;

class Poster
{
    public function posterList(Request $request)
    {
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $posterModel = new PosterModel();
        $posterList = $posterModel->order('sort', 'asc')->limit($start, $limit)->select();
        $count = $posterModel->order('sort', 'asc')->count();
        return adminSuccess('轮播图', ['list' => $posterList, 'count' => $count]);
    }

    public function addPoster(Request $request)
    {
        $param = $request->param();
        $data = [
            'type' => $param['type'],
            'sort' => $param['sort'],
            'jump_way' => $param['jump_way'],
            'img' => $param['img'],
            'link' => $param['link'],
            'create_time' => time(),
        ];
        if (isset($param['applet_id']) && !empty($param['applet_id'])) {
            $data['applet_id'] = $param['applet_id'];
        }
        $posterModel = new PosterModel();
        $posterModel->insert($data);
        return adminSuccess('添加成功');
    }

    public function editPoster(Request $request)
    {
        $param = $request->param();
        $data = [
            'type' => $param['type'],
            'sort' => $param['sort'],
            'jump_way' => $param['jump_way'],
            'img' => $param['img'],
            'link' => $param['link'],

        ];
        if (isset($param['applet_id']) && !empty($param['applet_id'])) {
            $data['applet_id'] = $param['applet_id'];
        }
        $posterModel = new PosterModel();
        $posterModel->where(['id' => $param['id']])->update($data);
        return adminSuccess('修改成功');
    }

    public function delPoster(Request $request)
    {
        $param = $request->param();
        $posterModel = new PosterModel();
        $posterModel->where(['id' => $param['id']])->delete();
        return adminSuccess('删除成功');
    }
}